﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GdChatViewController.h"
#include "../Views/GDChatItemView.h"
#include "../Util/GDUtil.h"
#include "../Util/GDHttpUtil.h"
#include "../GdDefine.h"

GdChatViewController::GdChatViewController(void)
:scrollView(NULL)
, sendNode(NULL)
, curHeight(0)
{
	curHeight = _px(10);
}


GdChatViewController::~GdChatViewController(void)
{
}

bool GdChatViewController::init(){
	if (!CAViewController::init()){
		return false;
	}
	return true;
}

void GdChatViewController::viewDidLoad(){
	winSize = this->getView()->getBounds().size;

	int sendNodeHeight = _px(100);

	scrollView = CAScrollView::createWithFrame(CCRect(0, 0, winSize.width, winSize.height - sendNodeHeight));
	scrollView->setBounceHorizontal(false);
	scrollView->setHorizontalScrollEnabled(false);
	scrollView->setShowsHorizontalScrollIndicator(false);
	this->getView()->addSubview(scrollView);

	sendNode = CAView::createWithFrame(CCRect(0, scrollView->getFrameOrigin().y + scrollView->getBounds().size.height, winSize.width, sendNodeHeight),CAColor_gray);
	this->getView()->addSubview(sendNode);

	inputField = CATextField::createWithFrame(CCRect(_px(20), _px(20), winSize.width - _px(140), _px(60)));
	inputField->setPlaceHolder("请输入聊天内容");
	sendNode->addSubview(inputField);

	CAButton *sendBtn = CAButton::createWithFrame(CCRect(inputField->getFrameOrigin().x + inputField->getBounds().size.width + _px(20), _px(20), _px(80), _px(60)), CAButtonTypeRoundedRect);
	sendBtn->setTitleForState(CAControlStateAll, "发送");
	sendNode->addSubview(sendBtn);
	sendBtn->addTarget(this, CAControl_selector(GdChatViewController::onSendBtnClick), CAControlEventTouchUpInSide);

	addNewMsg("你好，我是geelou，我是智能聊天机器人，让我们开始聊天吧。");
}

void GdChatViewController::onSendBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	std::string msgData = inputField->getText();

	if (msgData.empty() || msgData == ""){
		GDUtil::alert("请输入聊天内容");
		return;
	}

	addNewMsg(msgData,true);
	inputField->setText("");

	std::string url = GD_DOMAIN + "Chat.do?chat=t";
	map<std::string, std::string> key_value;

	key_value["FromUserName"] = "sys";
	key_value["MsgType"] = "text";
	key_value["ToUserName"] = "sys";
	key_value["CreateTime"] = "1234567890";
	key_value["Content"] = msgData;
	GDHttpUtil::getInstance()->send_post(url, key_value, this, CommonHttpJson_selector(GdChatViewController::onPostFinish), false);
}

void GdChatViewController::onPostFinish(const HttpResponseStatus& status, const CSJson::Value& json)
{
	if (IS_DEBUG) CCLog("返回数据：%s", json.toStyledString().c_str());
	if (json.isMember("replay")){
		addNewMsg(json["replay"].asString());
	}
}

void GdChatViewController::viewDidUnload(){

}
void GdChatViewController::viewDidAppear(){

}
void GdChatViewController::viewDidDisappear(){

}

void GdChatViewController::addNewMsg(std::string msg, bool isMe)
{
	GDChatItemView *itemView = GDChatItemView::createWithFrame(CCRect(0, curHeight, winSize.width, 80), msg, isMe);
	scrollView->addSubview(itemView);
	curHeight += itemView->getBounds().size.height + _px(10);
	scrollView->setViewSize(CCSize(scrollView->getBounds().size.width, curHeight + _px(20)));
	scrollToBtm();
}

void GdChatViewController::scrollToBtm()
{
	float offsetHeight = 0;

	if (curHeight > scrollView->getBounds().size.height){
		offsetHeight = curHeight - scrollView->getBounds().size.height;
	}
	scrollView->setContentOffset(CCPoint(0, offsetHeight),true);
}